package com.easyarlibrary.util;

import android.content.Context;
import android.content.res.AssetManager;
import android.util.Log;

import java.io.IOException;

import cn.easyar.FunctorOfVoidFromPointerOfTargetAndBool;
import cn.easyar.ImageTarget;
import cn.easyar.ImageTracker;
import cn.easyar.StorageType;
import cn.easyar.Target;

/**
 *
 * @author dell
 * @name： HelloARVideo
 */


public class LoadTargetUtil {

    private void loadFromImage(ImageTracker tracker, String path) {
        ImageTarget target = new ImageTarget();
        String jstr = "{\n"
                + "  \"images\" :\n"
                + "  [\n"
                + "    {\n"
                + "      \"image\" : \"" + path + "\",\n"
                + "      \"name\" : \"" + path.substring(0, path.indexOf(".")) + "\"\n"
                + "    }\n"
                + "  ]\n"
                + "}";
        target.setup(jstr, StorageType.Assets | StorageType.Json, "");
        tracker.loadTarget(target, new FunctorOfVoidFromPointerOfTargetAndBool() {
            @Override
            public void invoke(Target target, boolean status) {
                Log.i("HelloAR", String.format("load target (%b): %s (%d)", status, target.name(), target.runtimeID()));
            }
        });
    }


    public static void loadFromAssetsJsonFile(ImageTracker tracker, String path, String targetname) {
        ImageTarget target = new ImageTarget();
        target.setup(path, StorageType.Assets, targetname);
        tracker.loadTarget(target, new FunctorOfVoidFromPointerOfTargetAndBool() {
            @Override
            public void invoke(Target target, boolean status) {
                Log.i("HelloAR", String.format("load target (%b): %s (%d)", status, target.name(), target.runtimeID()));
            }
        });
    }

    public static void loadAllFromAssetsJsonFile(ImageTracker tracker, String path, int res) {
        for (ImageTarget target : ImageTarget.setupAll(path, StorageType.Assets)) {
            tracker.loadTarget(target, new FunctorOfVoidFromPointerOfTargetAndBool() {
                @Override
                public void invoke(Target target, boolean status) {
                    Log.i("HelloAR", String.format("load target (%b): %s (%d)", status, target.name(), target.runtimeID()));
                }
            });
        }
    }

    public static void loadAllFromAssetsJsonFile(ImageTracker tracker, String path) {
//        for(Target target : tracker.targets()){
//
//        }
        for (ImageTarget target : ImageTarget.setupAll(path, StorageType.Absolute)) {
            tracker.loadTarget(target, new FunctorOfVoidFromPointerOfTargetAndBool() {
                @Override
                public void invoke(Target target, boolean status) {
                    Log.i("HelloAR", String.format("load target (%b): %s (%d)", status, target.name(), target.runtimeID()));
                }
            });
        }
    }

    public static void loadAllFromFileJsonFile(ImageTracker tracker, String path) {
        for (ImageTarget target : ImageTarget.setupAll(path, StorageType.Absolute)) {
            tracker.loadTarget(target, new FunctorOfVoidFromPointerOfTargetAndBool() {
                @Override
                public void invoke(Target target, boolean status) {
                    Log.i("HelloAR", String.format("load target (%b): %s (%d)", status, target.name(), target.runtimeID()));
                }
            });
        }
    }

    public static String getTargetsFormAssets(Context context) {
        AssetManager assets = context.getAssets();
        String[] images = new String[0];
        String jsonContent = "";
        try {
            images = assets.list("");
            for (String name : images) {
                if (name.endsWith("jpg")
                        || name.endsWith("png")) {
                    jsonContent += "{\n" +
                            "      \"image\": \"" + name + "\"," +
                            "\"name\": \"" + name.split("\\.")[0] + "\""
                            + "},";
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            return "获取文件出错，请检查您的asses内资源文件";
        }
        jsonContent = jsonContent.substring(0, jsonContent.length() - 1);
        String targetsJson = "{\n" +
                "  \"images\": [" +
                jsonContent +
                "]\n" +
                "}";
        LogUtil.d("输出EasyAR需要的targets.json文件中的Json---->\n" + targetsJson);
        return targetsJson;
    }
}
